package com.app;

import com.fsoft.tinhcd.entity.Product;
import com.fsoft.tinhcd.util.HibernateUtils;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.hibernate.*;

import java.io.File;
import java.io.IOException;

public class MyApp {

    /**
     * @param args
     */
    static final File INDEX_FILE = new File("C:\\index");

    public static void main(String[] args) {
        createIndex();
    }

    public static void createData() {
        /*
         * Scanner scanner = new Scanner(System.in);
		 * System.out.println("Enter Name :"); String name = scanner.nextLine();
		 */
        Product product;
        Session session = HibernateUtils.getCurrentSession();
        Transaction tx = session.beginTransaction();
        for (int i = 0; i < 1000000; i++) {
            product = new Product();
            product.setId(String.valueOf(i));
            product.setName("name" + i);
            session.save(product);
            if (i % 20 == 0) { // 20, same as the JDBC batch size
                // flush a batch of inserts and release memory:
                session.flush();
                session.clear();
            }
        }

        tx.commit();
        session.close();
    }

    @SuppressWarnings("unchecked")
    private static void createIndex() {
        Product product;
        Session session = HibernateUtils.getCurrentSession();
        session.getTransaction().begin();
        Query query = session.createQuery("from Product");
        query.setReadOnly(true);
        query.setFetchSize(Integer.MIN_VALUE);
        ScrollableResults scrollableResults = query
                .scroll(ScrollMode.FORWARD_ONLY);
        StandardAnalyzer standardAnalyzer = new StandardAnalyzer(
                Version.LUCENE_44);
        Directory directory = null;
        try {
            directory = FSDirectory.open(INDEX_FILE);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(
                Version.LUCENE_44, standardAnalyzer);
        try {
            IndexWriter indexWriter = new IndexWriter(directory,
                    indexWriterConfig);
            while (scrollableResults.next()) {
                product = (Product) scrollableResults.get(0);
                addDocs(indexWriter, product);
                session.flush();
                session.clear();
            }
            indexWriter.commit();
            indexWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        session.getTransaction().commit();
    }

    private static void addDocs(IndexWriter indexWriter, Product product) {
        Document document = new Document();
        try {
            document.add(new StringField("name", product.getName(), Field.Store.YES));
            document.add(new StringField("id", product.getId(), Field.Store.YES));
            indexWriter.addDocument(document);
            System.out.println("Index id : " + product.getId() + " and Name : " + product.getName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
